package com.greenantmall.activity;

import java.util.List;

import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.bean.ServerSection;
import com.greenantmall.bean.ServiePhone;
import com.greenantmall.bean.StoreInfo;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.NotificationUtil;
import com.greenantmall.utils.UtilTools;

public class StartupActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.startup);
		this.init();
	}

	private void init() {
		RetrieveDataAsyn.getInstance().getServiePhone(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if (data != null) {
					ServiePhone serviePhone = (ServiePhone) data;
					AntHomeApplication.serviePhone = serviePhone;
					Log.d("CEXX", "CEXX ---> StartupActivity ---> ServiePhone : "
							+ AntHomeApplication.serviePhone.getPhone());
				}
			}
		});
		
		new Thread() {
			@Override
			public void run() {
				RetrieveDataAsyn.getInstance().registerAVClass(StartupActivity.this);
				NotificationUtil.registerNotificationReceiver(StartupActivity.this);
				RetrieveDataAsyn.getInstance().getStoreInfos(0, null);
				AntHomeApplication.soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
				
				Const.mStoreInfoCvs = null;
				if(AVUser.getCurrentUser() != null) {
					RetrieveDataAsyn.getInstance().getStoreInfosForStore(-1, new RetrieveDataListener() {
						@Override
						public void dataRetrieved(int requestId, Object data) {
							if(data != null) {
								Const.mStoreInfoCvs = (StoreInfo) data;
							}
						}
					});
					
					try {
						AVUser.getCurrentUser().refresh();
					} catch (AVException e) {
						e.printStackTrace();
					}
				}

				AntHomeApplication.soundPool.load(AntHomeApplication.context, R.raw.facebook_ringtone_pop, 1);
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				final ServerSection serverSection = UtilTools.getServerSection(getApplication());
				int sectionNum = serverSection.getSectionnum();
				Log.d("CEXX", "CEXX ---> StartupActivity ---> sectionNum : " + sectionNum);
				if (sectionNum == -1) {
					Intent intent = new Intent(StartupActivity.this, SelectAreaActivity.class);
					StartupActivity.this.startActivity(intent);
					StartupActivity.this.finish();
				} else {
					AntHomeApplication.sectionNum = sectionNum;

					RetrieveDataAsyn.getInstance().getServerSectionById(serverSection.getObjectId(), -1, 
							new RetrieveDataListener() {
						@Override
						public void dataRetrieved(int requestId, Object data) {
							List<ServerSection> list = (List<ServerSection>)data;
							if(list.size() > 0) {
								UtilTools.saveServerSection(getApplication(), list.get(0));
							}
							
							StartupActivity.this.finish();
						}
					});
					
					UtilTools.processUserChangeShoppingCart(StartupActivity.this);
					Intent intent = new Intent(StartupActivity.this, PortalActivity.class);
					StartupActivity.this.startActivity(intent);
				}
			}
		}.start();
	}

}
